package com.jozen.smart_toilet_system.tagView;

import android.content.Context;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;

import com.jozen.modelbase.netwoek.base.ModuleBaseApplication;
import com.jozen.modelbase.netwoek.util.DensityUtil;
import com.jozen.modelbase.netwoek.util.GlideUtil;
import com.jozen.smart_toilet_system.R;


/**
 * 动态添加的tagView
 */
public class PictureTagView extends RelativeLayout implements OnEditorActionListener {

    private Context context;
    private ImageView iv_bg;

    public enum Status {Normal, Edit}

    public enum Direction {Left, Right}

    private Direction direction = Direction.Left;
    private InputMethodManager imm;
    private static final int ViewWidth = DensityUtil.dip2px(ModuleBaseApplication.getContext(), 50);
    private static final int ViewHeight = DensityUtil.dip2px(ModuleBaseApplication.getContext(), 50);


    public PictureTagView(Context context, Direction direction) {
        super(context);
        this.context = context;
        this.direction = direction;
        initViews();
    }

    /**
     * 初始化视图
     **/
    protected void initViews() {
        LayoutInflater.from(context).inflate(R.layout.picturetagview, this, true);
        iv_bg = (ImageView) findViewById(R.id.iv_bg);
        iv_bg.setVisibility(VISIBLE);
    }


    public void setStatus(Status status) {
        switch (status) {
            case Normal:
//			tvPictureTagLabel.setVisibility(View.VISIBLE);
//			etPictureTagLabel.clearFocus();
//			tvPictureTagLabel.setText(etPictureTagLabel.getText());
//			etPictureTagLabel.setVisibility(View.GONE);
//			//隐藏键盘
//			imm.hideSoftInputFromWindow(etPictureTagLabel.getWindowToken() , 0);
                break;
            case Edit:
//			tvPictureTagLabel.setVisibility(View.GONE);
//			etPictureTagLabel.setVisibility(View.VISIBLE);
//			etPictureTagLabel.requestFocus();
//			//弹出键盘
//			imm.toggleSoftInput(0, InputMethodManager.SHOW_FORCED);
                break;
        }
    }

    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        setStatus(Status.Normal);
        return true;
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
        View parent = (View) getParent();
        int halfParentW = (int) (parent.getWidth() * 0.5);
        int center = (int) (l + (this.getWidth() * 0.5));
        if (center <= halfParentW) {
            direction = Direction.Left;
        } else {
            direction = Direction.Right;
        }
        directionChange();
    }

    private void directionChange() {
        switch (direction) {
            case Left:
//                loTag.setBackgroundResource(R.drawable.ic_home_water);
                break;
            case Right:
//                loTag.setBackgroundResource(R.drawable.ic_home_smoke);
                break;
        }
    }

    /**
     * @param state 配置默认是都空闲
     * @param isSos 是否是SOS第三卫生间紧急按钮
     * @return
     */
    public int getBgIcon(int state, boolean isSos) {
        int imgID = 0;
        if (isSos) {
            if (state == 0) {
                imgID = R.drawable.ic_sos_normal;
            } else if (state == 1) {
                imgID = R.drawable.ic_sos_alert;
            }
        } else {
            if (state == 0) {
                imgID = R.drawable.ic_squat_normal;
            } else if (state == 1) {
                imgID = R.drawable.ic_squat_use;
            }
        }
        return imgID;
    }

    /**
     * 动态设置icon
     *
     * @param imgID
     */
    public void setIcon(int imgID, boolean isSos, int state) {
        if (isSos) {
            if (state == 0) {
                iv_bg.setImageResource(imgID);
            } else {
                GlideUtil.loadLocalGif(context, imgID, iv_bg);
            }
        } else {
            iv_bg.setImageResource(imgID);
        }
    }


    public static int getViewWidth() {
        return ViewWidth;
    }

    public static int getViewHeight() {
        return ViewHeight;
    }
}
